﻿using netDxf;
using netDxf.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Merge
{
    public class NoDxf : IDxf
    {
        public void Add3Lines(DxfDocument doc, double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2, double cx1, double cy1, double cx2, double cy2, bool appendAnotherVerticalLine)
        {
        }

        public void AddFolderIndexLine(DxfDocument doc, int fullDir2ToIndex, double gridLeftX, double gridButtomY, double gridWidth)
        {
        }

        public void AddText(DxfDocument doc, string s, double x, double y, double height)
        {
        }

        public void CalcCellRect(Cell cell, IEnumerable<EntityObject> entities, Action<string, bool> updateMsg)
        {
        }

        public void CloneDxf(Cell cell, Action<string, bool> updateMsg)
        {
            cell.CanRead = true;
        }

        public void MergeSizeType(double gridWidth, double gridHeight, IEnumerable<Cell> cells, string outputDxfFile, LinkLabel lbl, IMergeSettings settings, Action<string, bool> updateMsg, Action<LinkLabel, bool> UpdateLableColor, Filters filters, Dictionary<Filter, int> dicNameOutOrder, Dictionary<string, string> fullDir2ToConterDict, Dictionary<string, int> fullDir2ToIndexDict)
        {
            bool hasCells = cells.Any();
            UpdateLableColor(lbl, hasCells);
            if (!hasCells)
                return;


            FileInfo fi = new FileInfo($"{outputDxfFile.Replace(".dxf", "_材料分类情况.txt")}");
            if (fi.Exists)
            {
                fi.Delete();
            }
            fi.Create().Close();
            StreamWriter sw = fi.AppendText();
            var matchingCells = cells.Where(c => Utils.WithInFilter(filters, c ,settings));
            double totalHeight = gridHeight * dicNameOutOrder.Count();
            double y = totalHeight / 2;
            int dir2Counter = 0;



            foreach (var kv in dicNameOutOrder.OrderBy(x => x.Value))
            {
                Filter filter = kv.Key;
                string filterName = filter.Name;
                var rowItems = matchingCells.Where(c => c.FilterName == filterName).ToList();
                double x = 0;
                double top = y + gridHeight;
                if (rowItems.Any())
                {
                    sw.WriteLine($"{filter.OutputOrder} {filterName}===================");
                    for (int currentIndex = 0; currentIndex < rowItems.Count; currentIndex++)
                    {
                        Cell cell = rowItems[currentIndex];
                        string fn = Path.GetFileNameWithoutExtension(cell.DxfFullName);
                        string par = Path.GetDirectoryName(cell.DxfFullName);
                        //sw.WriteLine($"{cell.DxfFullName}  ");
                        sw.WriteLine($"{fn,-50}{par,-100}");


                    }

                    sw.WriteLine();
                }




            }

            var exludedCells = cells.Where(c => !Utils.WithInFilter(filters, c, settings));
            if (exludedCells.Any())
            {

                sw.WriteLine($"其他分类===================");


                var exludedCellsList = exludedCells.ToList();
                for (int currentIndex = 0; currentIndex < exludedCellsList.Count; currentIndex++)
                {
                    Cell cell = exludedCellsList[currentIndex];


                    //sw.WriteLine($"{cell.DxfFullName}  ");
                    string fn = Path.GetFileNameWithoutExtension(cell.DxfFullName);
                    string par = Path.GetDirectoryName(cell.DxfFullName);
                    //sw.WriteLine($"{cell.DxfFullName}  ");
                    sw.WriteLine($"{fn,-50}{par,-100}");


                }
                sw.WriteLine();

                //non-grouped
            }
            sw.Close();
        }
        public DxfDocument ReplaceInsertAndRead(string dxfFile, string hash, Action<string, bool> updateMsg)
        {
            return null;
        }
    }
}
